<?php
//网络教研平台数据抓取
class eccpos extends control
{
	public function __construct()
	{
		header("Content-Type: text/html; charset=utf-8");
		set_time_limit(0); //设置不超时.
		parent::__construct();

		$this->eccpos_url = 'https://www.eccpos.com/';

		$this->p = $this->app->loadClass('proving');;
		$this->p->cfile = 'saoma_cookie.txt';
		$this->headers = array(
			"user-agent:  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
		);
	}


	//测试
	//http://127.0.0.1/www/get_data/pds/eccpos-testaa
	public function testaa()
	{
		$a = substr('2020083',0,6);
		a($a);
		echo 'll';
	}

	//登录页面
	public function loginpage()
	{
		//$this->view->rone_account = $rone_account;
		//$this->view->rone_pwd = $rone_pwd;
		$this->view->username = "NYPOSADMIN";
		$this->view->password = "inspur@2020";


		$this->display();
	}


	//获取token
	public function get_token()
	{

		$en_ps = $_POST['en_ps'];
		$MRT = $_POST['MRT'];
		$data = $_POST['data'];

		$arr = explode('%', $data);
		$arr[72] = '253CpasswordStatus';
		$arr[75] = '252FpasswordStatus';
		$arr[134] = '253CcomShort';
		$arr[146] = '252FcomShort';
		$data = implode('%', $arr);
		$data = 'UTF8' . $data;

		$hear_url = $MRT . "/sttokenconsumer?xmlStr=" . $data . "&relayState=" . $MRT . "/stlogin/gohomepage&en_ps=" . $en_ps;

		if ($hear_url) {
			$head = $this->p->curl($hear_url, 'GET', array(), false, 2);
		}
		a($head);
	}




	//获取 门店扫码时段统计
	public function ecclist()
	{
		//$this->loginpage();

		$url = 'https://www.eccpos.com/ortweb/analysis/custhour/getList';

		$headers = array(
			"user-agent:  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
		);
		$month = $this->getlastMonthDays();

		$post_data = array(
			'draw' => 1,
			'start' => 0,
			'length' => '25',
			'beginDate' => $month[0],
			'endDate' => $month[1],
		);

		$res = $this->p->curl($url, 'POST', $post_data, false, 1, $headers);
		//a($post_data);
		exit;
		$res = json_decode($res);
		//总条数
		$recordsTotal = $res->recordsTotal;
		a($recordsTotal);
		//记录总条数 要加日志

		$tol_page = ceil($recordsTotal / 25);


		for ($i = 0; $i < $tol_page; $i++) {
			$page_data = array(
				'draw' => $i + 1,
				'start' => $i * 25,
				'length' => '25',
				'beginDate' => $month[0],
				'endDate' => $month[1],
			);
			a($page_data);
			$res = $this->p->curl($url, 'POST', $page_data, false, 1, $headers);
			if ($res) {
				$res = json_decode($res);
				$this->eccpos->insert_saoma_datas($res->datas, $month[2]);
			}
		}
	}
//客户得分
public function customer_score(){
	
	//	查询条件
	
	 //  $period_url='https://www.eccpos.com/ortweb/business/period/getList';
		// $post_data = array(
		// 	'month' => '202008',		
		// );
  //      $res = $this->p->curl($url, 'POST', $post_data, false, 1, $headers);
       	//2020年8月第3周

	$res ='{"dataList":[{"WEEK_NAME":"2020年8月第3周（08.17-08.23）","WEEK":"2020083","END_DATE":"20200823","BEGIN_DATE":"20200817"},{"WEEK_NAME":"2020年8月第2周（08.10-08.16）","WEEK":"2020082","END_DATE":"20200816","BEGIN_DATE":"20200810"},{"WEEK_NAME":"2020年8月第1周（08.03-08.09）","WEEK":"2020081","END_DATE":"20200809","BEGIN_DATE":"20200803"}],"status":"000","dataMap":null,"msg":"执行成功！"}';
	$res =json_decode($res);	

	 if($res->status=="000" && 	$res->dataList){
		   foreach($res->dataList as $v){
				$this->customer_score_for($v->WEEK,$v->WEEK_NAME);
			 }

	}
	   	 

}

//客户得分
public function customer_score_for($week,$week_name){
	//客户得分
	$ke_url='http://www.eccpos.com/ortweb/operation/indexcalcscore/getShopIndexDatas';
	$post_data = array(
						'draw' => 1,
						'start' => 0,
						'length' => '50',
						'tagId'=> '00cbda8435adf3424bbd21224263963283',
						'taperiodSearchId'=>$week,

			);
	$res = $this->p->curl($ke_url, 'POST', $post_data, false, 1, $headers);
	$res =json_decode($res);

	
	if ($res) {
		$month = substr($week,0,6);
		$week_num =substr($week,6);
			$this->eccpos->insert_shop_score($res->datas,$week_name,$month,$week_num); 
		}

}



/*




https://www.eccpos.com/ortweb/operation/indexcalcscore/getShopIndexDatas

start: 0
length: 25
search[value]:
search[regex]: false
tagId: 00cbda8435adf3424bbd21224263963283
periodSearch: 2020083


        "TAG_SCORE": "96.00",
        "LICENSE_CODE": "411301100045",
        "cust_sale_num_daymean_week": 40,
        "cust_goods_num_oneord_max_t_week": 10,
        "cust_sale_amt_oneord_max_t_week": 10,
        "cust_sale_amt_hour_max_t_week": 10,
        "cust_goods_num_negative_t_week": 6,
        "SHOP_NAME": "高静-中心名酒行",
        "SHOP_ID": "411301100045",
        "TAG_SCORE_DESC": "金牌客户",
        "TENANT_ID": "411301100045",
        "cust_connect_times_daymean_week": 20




[{ title: '客户编码', data: 'LICENSE_CODE',  '},{ title: '客户名称', data: 'SHOP_NAME', },
		{ title: '客户评价', data: 'TAG_SCORE_DESC', },{ title: '客户得分', data: 'TAG_SCORE', }];



会员消费明细
url https://www.eccpos.com/mrtrs/mrt/report/getData

查询字段
	draw: 1
	start: 0
	length: 25
	search[value]:
	search[regex]: false
	isFirst: 1
	VIP_NAME:
	BEGIN_DATE: 20200825
	END_DATE: 20200826
	SHOPID:
	reportCode: f4b2158f-b862-41b5-bf1a-d79aa2bcddca
数据	amoney: 350
	amount: 1
	barcode: "6901028188029"
	crea_date: "20200825175435"
	glide_no: "58512008250100013"
	gods_name: "黄鹤楼(硬峡谷情细支)（条）"
	license_code: "411322105851"
	sale_dept: "方城第二客户服务部"
	shop_id: "411322105851"
	shop_name: "蔡洪恩-方城县老蔡副食部"
	slsman: "客户经理[张学武]"
	sort_name: "黄鹤楼"
	tel: "13333676998"
	unit_name: "条"
	user_name: "陆子豪"

	*/



	//获取上月时间
	public  function getlastMonthDays()
	{
		//上月第一天
		$firstday = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
		//上月最后一日 本月一日减一天即是上月最后一日
		$lastday = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400);

		$month = date('Y-m', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));

		return array($firstday, $lastday, $month);
	}



	//http请求
	public function http_request_get($lurl)
	{
		$cookie_file = dirname(__FILE__) . '/cookie.txt';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $lurl);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
		$res = curl_exec($ch);
		curl_close($ch);

		return $res;
	}

	//http请求POST
	public function http_request_post($lurl, $post_data)
	{
		$cookie_file = dirname(__FILE__) . '/cookie.txt';

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $lurl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
		$res = curl_exec($ch);
		curl_close($ch);

		return $res;
	}

	//http请求POST
	public function http_request_post_no_cookie($lurl, $post_data)
	{

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $lurl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$res = curl_exec($ch);
		curl_close($ch);

		return $res;
	}













	//get获取数据
	public function get_data($url)
	{
		//初始化
		$ch = curl_init();
		//设置选项，包括URL
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		//执行并获取HTML文档内容
		$output = curl_exec($ch);
		//释放curl句柄
		curl_close($ch);
		return $output;
	}







	public function get_case_input_value($data, $name)
	{

		///[\s*?]name[\s*?]=[\s*?]"idCard".*[\s*?]value[\s*?]=[\s*?][\'\"](.*)[\'\"]/isU
		preg_match_all('/[\s*?]name[\s*?]=[\s*?]"' . $name . '".*[\s*?]value[\s*?]=[\s*?][\'\"](.*)[\'\"]/isU', $data, $val);
		return  @$val[1][0];
	}

	//获取input的value值
	public function get_input_value($data, $name)
	{

		preg_match_all('/name="' . $name . '".*value=[\'\"](.*)[\'\"]/isU', $data, $val);
		return  @$val[1][0];
	}

	public function postcurl($url, $data, $cookie_file)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
		//curl_setopt($ch, CURLOPT_COOKIE,$cookie);
		$result = curl_exec($ch);
		curl_close($ch);

		return $result;
	}
}
